package com.metaera.app.data;


import com.metaera.app.data.model.ActivityListResponse;
import com.metaera.app.data.model.AppConfigResponse;
import com.metaera.app.data.model.ArticleListResponse;
import com.metaera.app.data.model.BasicListResponse;
import com.metaera.app.data.model.CollectionListResponse;
import com.metaera.app.data.model.FeedbackResponse;
import com.metaera.app.data.model.FollowAuthorResponse;
import com.metaera.app.data.model.HistoryListResponse;
import com.metaera.app.data.model.HomeTopNewsResponse;
import com.metaera.app.data.model.HotColumnResponse;
import com.metaera.app.data.model.HotSearchResponse;
import com.metaera.app.data.model.InformationDetailsResponse;
import com.metaera.app.data.model.InvestFinanceResponse;
import com.metaera.app.data.model.MorningPostResponse;
import com.metaera.app.data.model.NoticeListResponse;
import com.metaera.app.data.model.PhoneCodeResponse;
import com.metaera.app.data.model.ProfileResponse;
import com.metaera.app.data.model.SearchResponse;
import com.metaera.app.data.model.SearchResultResponse;
import com.metaera.app.data.model.SpecialDetailsResponse;
import com.metaera.app.data.model.SpecialResponse;
import com.metaera.app.data.model.TodayEventResponse;
import com.metaera.app.data.model.UploadImageResponse;
import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.data.model.UserNoticeResponse;
import com.metaera.app.data.model.VersionUpInfoResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.data.network.interceptor.CommonParameterInterceptor;
import com.metaera.app.data.model.BasicResponse;
import com.metaera.app.data.model.HomeNewsTypeResponse;
import com.metaera.app.data.network.WebServiceApi;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 远程/网络 数据源
 */
public class RemoteDataSource implements IDataSource {

    private final WebServiceApi mWebServiceApi;

    RemoteDataSource() {

        mWebServiceApi = initialize().create(WebServiceApi.class);
    }

    private Retrofit initialize() {
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(12, TimeUnit.SECONDS)
                .addInterceptor(new CommonParameterInterceptor())
                .addNetworkInterceptor(logInterceptor)
                .build();

        return new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(ApiParam.BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public Observable<BasicResponse<UploadImageResponse>> upload(File file) {
        Map<String, RequestBody> fileMap = new HashMap<>();
        fileMap.put("file" + "\";filename=\"" + file.getName(),
                RequestBody.create(file, MediaType.parse("image/png")));

        return mWebServiceApi.upload(fileMap);
    }

    @Override
    public Observable<BasicResponse<List<HotSearchResponse>>> hotSearch() {

        return mWebServiceApi.hotSearch();
    }

    @Override
    public Observable<BasicResponse<HomeTopNewsResponse>> homeTopNews() {
        return mWebServiceApi.homeTopNews();
    }

    @Override
    public Observable<BasicListResponse<MorningPostResponse>> getNewsDetails(int page, int size) {
        return mWebServiceApi.getNewsDetails(page, size);
    }

    @Override
    public Observable<BasicListResponse<InvestFinanceResponse>> getInvestDetails(int page, int size) {
        return mWebServiceApi.getInvestDetails(page, size);
    }

    @Override
    public Observable<BasicListResponse<TodayEventResponse>> getEventDetails(int page, int size) {
        return mWebServiceApi.getEventDetails(page, size);
    }

    @Override
    public Observable<BasicResponse<List<HomeNewsTypeResponse>>> categoryLists(int type) {

        return mWebServiceApi.categoryLists(type);
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> getArticleList(
            int category_id, int type, int page, int size) {

        return mWebServiceApi.getArticleList(category_id, type, page, size);
    }

    @Override
    public Observable<BasicResponse<UserDetailsResponse>> getUserInfo() {

        return mWebServiceApi.getUserInfo();
    }

    @Override
    public Observable<BasicResponse<ProfileResponse>> toUserInfo(int id) {

        return mWebServiceApi.toUserInfo(id);
    }

    @Override
    public Observable<BasicResponse<Object>> editUserInfo(
            String nickname, String avatar_img, String sex, String position, String desc,
            String weibo, String official_account, String twitter, String email,
            String is_show_phone, String phone, String knowledge_planet, String is_show_wechat, String wechat) {

        return mWebServiceApi.editUserInfo(nickname, avatar_img, sex, position, desc, weibo,
                official_account, twitter, email, is_show_phone, phone, knowledge_planet, is_show_wechat, wechat);
    }


    @Override
    public Observable<BasicResponse<UserDetailsResponse>> login(
            int type, String code, String mobile, String password, String phone_code) {

        return mWebServiceApi.login(type, code, mobile, password, phone_code);
    }

    @Override
    public Observable<BasicResponse<List<PhoneCodeResponse>>> getPhoneCode() {
        return mWebServiceApi.getPhoneCode();
    }

    @Override
    public Observable<BasicResponse<Object>> sendSms(String code, String mobile) {
        return mWebServiceApi.sendSms(code, mobile);
    }

    @Override
    public Observable<BasicResponse<Object>> setPassword(String code, String mobile, String phone_code, String password1, String password2) {
        return mWebServiceApi.setPassword(code, mobile, phone_code, password1, password2);
    }

    @Override
    public Observable<BasicResponse<Object>> forgotPassword(String code, String mobile, String phone_code, String password1, String password2) {
        return mWebServiceApi.forgotPassword(code, mobile, phone_code, password1, password2);
    }

    @Override
    public Observable<BasicListResponse<HistoryListResponse>> getHistoryList(int page, int size) {

        return mWebServiceApi.getHistoryList(page, size);
    }

    @Override
    public Observable<BasicListResponse<CollectionListResponse>> myCollection(int page, int size) {

        return mWebServiceApi.myCollection(page, size);
    }

    @Override
    public Observable<BasicListResponse<NoticeListResponse>> getNoticeList(int status, int page, int size) {

        return mWebServiceApi.getNoticeList(status, page, size);
    }

    @Override
    public Observable<BasicResponse<List<FeedbackResponse>>> getFeedbackLog() {

        return mWebServiceApi.getFeedbackLog();
    }

    @Override
    public Observable<BasicResponse<Object>> submitFeedback(int type, String content, String name, String contact) {

        return mWebServiceApi.submitFeedback(type, content, name, contact);
    }

    @Override
    public Observable<BasicResponse<Object>> cleanHistory() {

        return mWebServiceApi.cleanHistory();
    }

    @Override
    public Observable<BasicResponse<List<ArticleListResponse>>> relatedRecommend(String id) {

        return mWebServiceApi.relatedRecommend(id);
    }

    @Override
    public Observable<BasicResponse<InformationDetailsResponse>> getInformationDetails(String id) {

        return mWebServiceApi.getInformationDetails(id);
    }

    @Override
    public Observable<BasicListResponse<HotColumnResponse>> getPopularColumn(int type, int page, int size) {
        return mWebServiceApi.getPopularColumn(type, page, size);
    }

    @Override
    public Observable<BasicResponse<SpecialResponse>> getSpecial(int page, int size) {
        return mWebServiceApi.getSpecial(page, size);
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> appointDateCalendar(
            String date, int categoryID) {
        return mWebServiceApi.appointDateCalendar(date, categoryID);
    }

    @Override
    public Observable<BasicResponse<List<String>>> calendarList(String date, int categoryID) {
        return mWebServiceApi.calendarList(date, categoryID);
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> getUserInformation(String id, int page, int size) {
        return mWebServiceApi.getUserInformation(id, page, size);
    }

    @Override
    public Observable<BasicListResponse<ActivityListResponse>> getActivityList(int type, int page, int size) {
        return mWebServiceApi.getActivityList(type, page, size);
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> getFollowLists(int page, int size) {
        return mWebServiceApi.getFollowLists(page, size);
    }

    @Override
    public Observable<BasicResponse<ActivityListResponse>> getActivityDetails(int id) {
        return mWebServiceApi.getActivityDetails(id);
    }

    @Override
    public Observable<BasicResponse<AppConfigResponse>> getAppConfig() {
        return mWebServiceApi.getAppConfig();
    }

    @Override
    public Observable<BasicListResponse<SearchResultResponse>> searchMore(
            String key, int orderType, int timeType, int type, int page, int size) {
        return mWebServiceApi.searchMore(key, orderType, timeType, type, page, size);
    }

    @Override
    public Observable<BasicResponse<SearchResponse>> search(String key, int orderType, int timeType) {
        return mWebServiceApi.search(key, orderType, timeType);
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> getRecommendLists(int page, int size) {
        return mWebServiceApi.getRecommendLists(page, size);
    }

    @Override
    public Observable<BasicListResponse<ArticleListResponse>> getLabelDetails(int id, int page, int size) {
        return mWebServiceApi.getLabelDetails(id, page, size);
    }

    @Override
    public Observable<BasicResponse<Object>> followAuthor(int author_id, int is_status) {
        return mWebServiceApi.followAuthor(author_id, is_status);
    }

    @Override
    public Observable<BasicResponse<VersionUpInfoResponse>> versionUpInfo() {
        return mWebServiceApi.versionUpInfo();
    }

    @Override
    public Observable<BasicResponse<Object>> likeInformation(int id, int is_status, int type) {
        return mWebServiceApi.likeInformation(id, is_status, type);
    }

    @Override
    public Observable<BasicResponse<SpecialDetailsResponse>> getSpecialDetails(int id) {
        return mWebServiceApi.getSpecialDetails(id);
    }

    @Override
    public Observable<BasicResponse<UserDetailsResponse>> weChatLogin(String code) {
        return mWebServiceApi.weChatLogin(code);
    }

    @Override
    public Observable<BasicResponse<UserDetailsResponse>> bindMobile(
            String code, String mobile, String phone_code, String token) {
        return mWebServiceApi.bindMobile(code, mobile, phone_code, token);
    }

    @Override
    public Observable<BasicResponse<Object>> collectionInformation(int id) {
        return mWebServiceApi.collectionInformation(id);
    }

    @Override
    public Observable<BasicResponse<Object>> cancelCollectionInformation(int id) {
        return mWebServiceApi.cancelCollectionInformation(id);
    }

    @Override
    public Observable<BasicListResponse<FollowAuthorResponse>> getFollowList(int page, int size) {
        return mWebServiceApi.getFollowList(page, size);
    }

    @Override
    public Observable<BasicResponse<Object>> updateAuthorNotice(int id, int is_notice) {
        return mWebServiceApi.updateAuthorNotice(id, is_notice);
    }

    @Override
    public Observable<BasicResponse<UserNoticeResponse>> getUserNotice() {
        return mWebServiceApi.getUserNotice();
    }

    @Override
    public Observable<BasicResponse<Object>> updateUserNotice(
            int id, String is_new_notice, String is_disturb, String disturb_start_time, String disturb_end_time,
            String is_article, String is_news_flash, String is_notice, String is_author_update) {

        return mWebServiceApi.updateUserNotice(is_new_notice, is_disturb, disturb_start_time,
                disturb_end_time, is_article, is_news_flash, is_notice, is_author_update,id);
    }

    @Override
    public Observable<BasicResponse<Object>> logOff() {
        return mWebServiceApi.logOff();
    }
}
